using System;
using System.Web;
using newtelligence.DasBlog.Runtime;
using newtelligence.DasBlog.Util;
using newtelligence.DasBlog.Web.Core;
using newtelligence.DasBlog.Web.Services.Rss20;
using System.Xml;
using System.Xml.Serialization;

namespace newtelligence.DasBlog.Web.Services
{
	public class GoogleSitemapHandler : IHttpHandler
	{
		public GoogleSitemapHandler()
		{
		}


        public void ProcessRequest( HttpContext context )
        {
            try
            {
				//Cache the sitemap for 12 hours...
				string CacheKey = "GoogleSiteMap";
				urlset root = HttpContext.Current.Cache[CacheKey] as urlset;
				if (root == null) //we'll have to build it...
				{
					ILoggingDataService logService = LoggingDataServiceFactory.GetService(SiteConfig.GetLogPathFromCurrentContext());
					IBlogDataService dataService = BlogDataServiceFactory.GetService(SiteConfig.GetContentPathFromCurrentContext(), logService);
					SiteConfig siteConfig = SiteConfig.GetSiteConfig();

					root = new urlset();

					root.url = new urlCollection();

					//Default first...
					url basePage = new url(Utils.GetBaseUrl(siteConfig),DateTime.Now,changefreq.daily,1.0M);
					root.url.Add(basePage);

					url defaultPage = new url(Utils.GetStartPageUrl(siteConfig),DateTime.Now,changefreq.daily,1.0M);
					root.url.Add(defaultPage);

					//Archives next...
					url archivePage = new url(Utils.RelativeToRoot(siteConfig, "archives.aspx"),DateTime.Now,changefreq.daily,1.0M);
					root.url.Add(archivePage);

					//All Pages
					EntryIdCache entryCache = dataService.GetEntryIdCache();
					foreach(EntryIdCacheEntry e in entryCache.Entries)
					{
						if (e.IsPublic)
						{
							//Start with a RARE change freq...newer posts are more likely to change more often.
							// The older a post, the less likely it is to change...
							changefreq freq = changefreq.daily;						

							//new stuff?
							if(e.DateLocalTime < DateTime.Now.AddMonths(-9))
							{
								freq = changefreq.yearly;
							}
							else if(e.DateLocalTime < DateTime.Now.AddDays(-30))
							{
								freq = changefreq.monthly;
							}
							else if (e.DateLocalTime < DateTime.Now.AddDays(-7))
							{
								freq = changefreq.weekly;
							}
							if (e.DateLocalTime > DateTime.Now.AddDays(-2))
							{
								freq = changefreq.hourly;
							}

							//Add comments pages, since comments have indexable content...
							// Only add comments if we aren't showing comments on permalink pages already
							if (siteConfig.ShowCommentsWhenViewingEntry == false)
							{
								url commentPage = new url(Utils.GetCommentViewUrl(siteConfig,e.EntryId),e.DateLocalTime,freq,0.7M);
								root.url.Add(commentPage);
							}

							//then add permalinks
							url permaPage = new url(Utils.GetPermaLinkUrl(siteConfig,e),e.DateLocalTime,freq,0.9M);
							root.url.Add(permaPage);
						}
					}

					//All Categories
					CategoryCacheEntryCollection catCache = dataService.GetCategories();
					foreach (CategoryCacheEntry cce in catCache)
					{
						if (cce.IsPublic)
						{
							url catPage = new url(Utils.GetCategoryViewUrl(siteConfig,cce.Name),DateTime.Now,changefreq.weekly,0.6M);
							root.url.Add(catPage);
						}
					}
					HttpContext.Current.Cache.Insert(CacheKey,root,null,DateTime.Now.AddHours(12),System.Web.Caching.Cache.NoSlidingExpiration);
				}

				XmlSerializer x = new XmlSerializer(typeof(urlset));
				x.Serialize(HttpContext.Current.Response.OutputStream,root);
				HttpContext.Current.Response.ContentType = "text/xml";
			}
            catch(Exception exc)
            {
                ErrorTrace.Trace(System.Diagnostics.TraceLevel.Error,exc);
            }
        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
	}
}
